package my.xh.validate.handler.impl;

import my.xh.validate.ValidateField;
import my.xh.validate.handler.ValidateHandler;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName MaxLenValidateHandler
 * @Date 2019/1/17 15:03
 * @Author xuanhu
 * @Description 字符串最大长度校验
 */
@Component
public class MaxLenValidateHandler implements ValidateHandler {
    @Override
    public boolean validate(ValidateField validateField, StringBuilder paramName, Object paramValue, Map<String, Object> map, boolean flag) {

        if (validateField.maxLen() > 0) { // 判断字符串最大长度
            if (((String) paramValue).length() > validateField.maxLen()) {
                flag = false;
                map.put(paramName.toString(), getMsg(validateField, paramName.append(" 的长度不能大于").append(validateField.maxLen()).toString()));
            }
        }
        return flag;
    }
}
